<?php
/**
 * Setup theme functions for Sento.
 *
 * @package Sento
 */

// Setup content width 
if ( ! isset( $content_width ) )
	$content_width = 1170;

//----------------------------------------------------------------------------------
//	Add Theme Options Panel & Assign Variable Values
//----------------------------------------------------------------------------------

	// Add Redux Framework - Credits attributable to http://reduxframework.com/
	require_once (get_template_directory() . '/admin/main/framework.php');

	// Load Theme Variables.
	require_once( get_template_directory() . '/admin/main/options/00.variables.php' ); 

	// Add Theme Options Features.
	require_once( get_template_directory() . '/admin/main/options/00.theme-setup.php' ); 
	require_once( get_template_directory() . '/admin/main/options/01.general-settings.php' ); 
	require_once( get_template_directory() . '/admin/main/options/02.homepage.php' ); 
	require_once( get_template_directory() . '/admin/main/options/03.header.php' ); 
	require_once( get_template_directory() . '/admin/main/options/04.footer.php' );
	require_once( get_template_directory() . '/admin/main/options/05.blog.php' ); 
	require_once( get_template_directory() . '/admin/main/options/08.special-pages.php' ); 
	require_once( get_template_directory() . '/lib/simple_html_dom.php' );
	
//----------------------------------------------------------------------------------
//	Assign Theme Specific Functions
//----------------------------------------------------------------------------------

// Setup theme features, register menus and scripts.
if ( ! function_exists( 'thinkup_themesetup' ) ) {

	function thinkup_themesetup() {

		// Load required files
		require_once ( get_template_directory() . '/lib/functions/extras.php' );
		require_once ( get_template_directory() . '/lib/functions/template-tags.php' );

		// Make theme translation ready.
		//sento 翻译
		load_theme_textdomain( 'sento', get_template_directory() . '/languages' );
		//redux-framework 翻译
		load_theme_textdomain( 'redux-framework', get_template_directory() . '/languages' );
		
		// Add default theme functions.
		add_theme_support( 'automatic-feed-links' );
		add_theme_support( 'post-thumbnails' );
		add_theme_support( 'post-formats', array( 'gallery', 'image', 'video', 'audio', 'status', 'quote', 'link', 'chat' ) );
		add_theme_support( 'custom-background' );

		// Add support for custom header
		$args = apply_filters( 'custom-header', array( 'height' => 200, 'width'  => 1600 ) );
		add_theme_support( 'custom-header', $args );

		// Add WooCommerce functions.
		add_theme_support( 'woocommerce' );

		// Register theme menu's.
		register_nav_menus( array( 'pre_header_menu' => 'Pre Header Menu', ) );
		register_nav_menus( array( 'header_menu' => 'Primary Header Menu', ) );
		register_nav_menus( array( 'sub_footer_menu' => 'Footer Menu', ) );
	}
}
add_action( 'after_setup_theme', 'thinkup_themesetup' );


//----------------------------------------------------------------------------------
//	Register Front-End Styles And Scripts
//----------------------------------------------------------------------------------

function thinkup_frontscripts() {

	// Add jQuery library.
	wp_enqueue_script('jquery');

	// Register theme stylesheets.
	wp_register_style( 'thinkup-responsive', get_template_directory_uri() . '/styles/style-responsive.css', '', '1.1' );
	wp_register_style( 'thinkup-sidebarleft', get_template_directory_uri() . '/styles/layouts/thinkup-left-sidebar.css', '', '1.1' );
	wp_register_style( 'thinkup-sidebarright', get_template_directory_uri() . '/styles/layouts/thinkup-right-sidebar.css', '', '1.1' );

	// Add theme stylesheets.
	wp_enqueue_style( 'thinkup-bootstrap', get_template_directory_uri() . '/lib/extentions/bootstrap-3.3.5/css/bootstrap.min.css', '', '3.3.5' );
	//wp_enqueue_style( 'thinkup-bootstrap-theme', get_template_directory_uri() . '/lib/extentions/bootstrap-3.3.5/css/bootstrap-theme.min.css', '', '3.3.5' );
	wp_enqueue_style( 'thinkup-prettyPhoto', get_template_directory_uri() . '/lib/extentions/prettyPhoto/css/prettyPhoto.css', '', '3.1.6' );
	wp_enqueue_style( 'thinkup-shortcodes', get_template_directory_uri() . '/styles/style-shortcodes.css', '', '1.1' );
	wp_enqueue_style( 'thinkup-style', get_stylesheet_uri(), '', '1.0.5' );

	// Add Font Packages.
	wp_enqueue_style( 'dashicons' );
	wp_enqueue_style( 'thinkup-font-awesome', get_template_directory_uri() . '/lib/extentions/font-awesome/css/font-awesome.css', '', '4.2.0' );

	// Add theme scripts
	wp_enqueue_script( 'thinkup-imagesloaded', get_template_directory_uri() . '/lib/scripts/plugins/imagesloaded/imagesloaded.js', array( 'jquery' ), '1.3.4', true );
	wp_enqueue_script( 'thinkup-prettyPhoto', ( get_template_directory_uri().'/lib/extentions/prettyPhoto/js/jquery.prettyPhoto.js' ), array( 'jquery' ), '3.1.6', 'true' );
	wp_enqueue_script( 'thinkup-frontend', get_template_directory_uri() . '/lib/scripts/main-frontend.js', array( 'jquery' ), '1.1', 'true'  );
	wp_enqueue_script( 'thinkup-bootstrap', get_template_directory_uri() . '/lib/extentions/bootstrap-3.3.5/js/bootstrap.min.js', array( 'jquery' ), '3.3.5', 'true'  );
	wp_enqueue_script( 'thinkup-modernizr', get_template_directory_uri() . '/lib/scripts/modernizr.js', array( 'jquery' ), '2.6.2', 'true'  );

	// Register Retina script - Enqueued in 01.general-settings.php
	wp_register_script( 'thinkup-retina', get_template_directory_uri() . '/lib/scripts/retina.js', array( 'jquery' ), '0.0.2', '', true );

	// Add Masonry scripts to Blog & Testimonials pag
	if ( thinkup_check_isblog() ) {
		wp_enqueue_script( 'thinkup-masonry', get_template_directory_uri() . '/lib/scripts/plugins/masonry/masonry.js', array( 'jquery' ), '3.1.2', true );
	}

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}

	// Add ThinkUpSlider scripts
	if ( is_front_page() or thinkup_check_ishome() ) {
		wp_enqueue_script( 'thinkup-slider', get_template_directory_uri() . '/lib/scripts/plugins/ResponsiveSlides/responsiveslides.js', array( 'jquery' ), '1.54', 'true' );
		wp_enqueue_script( 'thinkup-slider-call', get_template_directory_uri() . '/lib/scripts/plugins/ResponsiveSlides/responsiveslides-call.js', array( 'jquery' ), '', 'true' );
	}
}
add_action( 'wp_enqueue_scripts', 'thinkup_frontscripts', 10 );


//----------------------------------------------------------------------------------
//	Register Back-End Styles And Scripts
//----------------------------------------------------------------------------------

function thinkup_adminscripts() {

	// Register theme stylesheets.
	wp_register_style( 'thinkup-backend', get_template_directory_uri() . '/styles/backend/style-backend.css', '', 1.1 );
	wp_register_style( 'thinkup-font-awesome-backend', get_template_directory_uri() . '/lib/extentions/font-awesome/css/font-awesome.css', '', '4.2.0' );

	// Register theme scripts.
	wp_register_script( 'thinkup-backend', get_template_directory_uri() . '/lib/scripts/main-backend.js', array( 'jquery' ), '1.1' );

		// Add theme stylesheets.
		wp_enqueue_style( 'thinkup-backend' );
		wp_enqueue_style( 'thinkup-font-awesome-backend' );

		// Add theme scripts.
		wp_enqueue_script( 'thinkup-backend' );
}
add_action( 'admin_enqueue_scripts', 'thinkup_adminscripts' );


//----------------------------------------------------------------------------------
//	Register Theme Widgets
//----------------------------------------------------------------------------------

function thinkup_widgets_init() {

	// Register default sidebar
	register_sidebar( array(
		'name' => __( 'Sidebar', 'sento' ),
		'id' => 'sidebar-1',
		'before_widget' => '<aside class="widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	// Register HomePage sidebar
	register_sidebar( array(
			'name' => __( 'HomePageSidebar', 'sento' ),
			'id' => 'sidebar-2',
			'before_widget' => '<aside class="widget %2$s">',
			'after_widget' => '</aside>',
			'before_title' => '<h3 class="widget-title">',
			'after_title' => '</h3>',
	) );	
	
	// Register Post sidebar
	register_sidebar( array(
			'name' => __( 'PostSidebar', 'sento' ),
			'id' => 'sidebar-3',
			'before_widget' => '<aside class="widget %2$s">',
			'after_widget' => '</aside>',
			'before_title' => '<h3 class="widget-title">',
			'after_title' => '</h3>',
	) );
	
	// Register Blog sidebar
	register_sidebar( array(
			'name' => __( 'BlogSidebar', 'sento' ),
			'id' => 'sidebar-4',
			'before_widget' => '<aside class="widget %2$s">',
			'after_widget' => '</aside>',
			'before_title' => '<h3 class="widget-title">',
			'after_title' => '</h3>',
	) );
	
	// Register footer sidebars
    register_sidebar( array(
        'name' => __( 'Footer Column 1', 'sento' ),
        'id' => 'footer-w1',
        'before_widget' => '<aside class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="footer-widget-title"><span>',
        'after_title' => '</span></h3>',
    ) );
 
    register_sidebar( array(
        'name' => __( 'Footer Column 2', 'sento' ),
        'id' => 'footer-w2',
        'before_widget' => '<aside class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="footer-widget-title"><span>',
        'after_title' => '</span></h3>',
    ) );

    register_sidebar( array(
        'name' => __( 'Footer Column 3', 'sento' ),
        'id' => 'footer-w3',
        'before_widget' => '<aside class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="footer-widget-title"><span>',
        'after_title' => '</span></h3>',
    ) );

    register_sidebar( array(
        'name' => __( 'Footer Column 4', 'sento' ),
        'id' => 'footer-w4',
        'before_widget' => '<aside class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="footer-widget-title"><span>',
        'after_title' => '</span></h3>',
    ) );

    register_sidebar( array(
        'name' => __( 'Footer Column 5', 'sento' ),
        'id' => 'footer-w5',
        'before_widget' => '<aside class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="footer-widget-title"><span>',
        'after_title' => '</span></h3>',
    ) );

    register_sidebar( array(
        'name' => __( 'Footer Column 6', 'sento' ),
        'id' => 'footer-w6',
        'before_widget' => '<aside class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="footer-widget-title"><span>',
        'after_title' => '</span></h3>',
    ) );

	// Register sub-footer sidebars
    register_sidebar( array(
        'name' => __( 'Sub-Footer Column 1', 'sento' ),
        'id' => 'sub-footer-w1',
        'before_widget' => '<aside class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="sub-footer-widget-title"><span>',
        'after_title' => '</span></h3>',
    ) );

    register_sidebar( array(
        'name' => __( 'Sub-Footer Column 2', 'sento' ),
        'id' => 'sub-footer-w2',
        'before_widget' => '<aside class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="sub-footer-widget-title"><span>',
        'after_title' => '</span></h3>',
    ) );
    
    
    
    
}
add_action( 'widgets_init', 'thinkup_widgets_init' );

/**
 * 显示友情链接菜单
 */
add_filter('pre_option_link_manager_enabled','__return_true');

/**
 * 解决gravatar被墙
 * @param unknown $avatar
 * @return mixed
 */
function duoshuo_avatar($avatar) {
	$avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),"gravatar.duoshuo.com",$avatar);
	return $avatar;
}
add_filter( 'get_avatar', 'duoshuo_avatar', 10, 3 );


add_filter ( 'gettext_with_context', 'disable_open_sans', 888, 4 );
function disable_open_sans($translations, $text, $context, $domain) {
	if ('Open Sans font: on or off' == $context && 'on' == $text) {
		$translations = 'off';
	}
	return $translations;
}

function dw_remove_open_sans() {
	wp_deregister_style ( 'open-sans' );
	wp_register_style ( 'open-sans', false );
	wp_enqueue_style ( 'open-sans', '' );
}

add_action ( 'init', 'dw_remove_open_sans' );

add_action( 'show_admin_bar', '__return_false');

/**
 * 文章内容拦截器
 * 修复从博客园RSS获取的内容显示bug
 * @return mixed
 */
function fix_cnblogs_the_content(){
	//修复博客园文章BUG
	$doc = str_get_html(func_get_arg(0));
	$nodes = $doc -> find("[id^='div-gpt-ad']");
	foreach($nodes as $node){
		$node -> innertext = "";
	}
	$content = $doc -> outertext;
	$doc -> clear();
	return $content;
}

add_filter( 'the_content' , 'fix_cnblogs_the_content');